home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / finger-1 / my_units / myapplee.uni < prev    next >
Text File  |  1992-02-24  |  4KB  |  132 lines

  1. unit MyAppleEvents;
  2.  
  3. { This code is part of the Finger/Fingerd source code, written in THINK Pascal 4 }
  4. { Copyright 1991-1992 Peter N Lewis }
  5. { If you use this code, you must give me credit in your about box and documentation }
  6. { This is part of my generic library of routines }
  7.  
  8. interface
  9.  
  10.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  11. { function DoOApp: OSErr }
  12. { function DoODoc (fs: FSSpec): OSErr }
  13. { function DoPrint (fs: FSSpec): OSErr }
  14. { function DoQuit: OSErr}
  15.  
  16. implementation
  17.  
  18.     uses
  19.         AppleTalk, PPCToolbox, Processes, EPPC, Notification, AppleEvents, MyUtilities;
  20.  
  21.     function DoOApp (p: ptr): OSErr;
  22.     inline
  23.         $205F, $4E90;
  24.  
  25.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  26.     inline
  27.         $205F, $4E90;
  28.  
  29.     function DoQuit (p: ptr): OSErr;
  30.     inline
  31.         $205F, $4E90;
  32.  
  33.     const
  34.         kPatienceLevel = 1000;                                { <aevt> ticks we wait for response }
  35.         kSysEnvironsVersion = 1;
  36.         kOSEvent = app4Evt;    {event used by MultiFinder}
  37.         kSuspendResumeMessage = 1;        {high byte of suspend/resume event message}
  38.         kResumeMask = 1;        {bit of message field for resume vs. suspend}
  39.         kMouseMovedMessage = $FA;        {high byte of mouse-moved event message}
  40.         kNoEvents = 0;        {no events mask}
  41.         kNoListChosen = -1;
  42.  
  43.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;        { <aevt> }
  44.         var
  45.             typeCode: DescType;
  46.             actualSize: Size;
  47.             err: OSErr;
  48.     begin
  49.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);    { nil ok: need only function result }
  50.         if err = errAEDescNotFound then        { we got all the required params: all is ok }
  51.             GotRequiredParams := noErr
  52.         else if err = noErr then
  53.             GotRequiredParams := errAEEventNotHandled
  54.         else
  55.             GotRequiredParams := err;
  56.     end; { GotRequiredParams }
  57.  
  58.     function HandleOAPP (theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;{ <aevt> }
  59.         var
  60.             oe: OSErr;
  61.     begin
  62.     { We don't expect any params at all, but check in case the client requires any }
  63.         oe := GotRequiredParams(theAppleEvent);
  64.         oe := DoOApp(openappp);
  65.         HandleOAPP := oe;
  66.     end;
  67.  
  68.     function HandleDocs (theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr;        { <aevt> }
  69.         var
  70.             myFSS: FSSpec;
  71.             docList: AEDescList;
  72.             index, itemsInList: LONGINT;
  73.             actualSize: Size;
  74.             keywd: AEKeyword;
  75.             typeCode: descType;
  76.             ignoreWPtr: WindowPtr;
  77.             oe, ooe: OSErr;
  78.     begin
  79.         oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
  80.         if oe = noErr then begin
  81.             ooe := GotRequiredParams(theAppleEvent);
  82.     { now get each alias from the list (as an FSSSpec) and open the associated file. }
  83.             oe := AECountItems(docList, itemsInList);
  84.             for index := 1 to itemsInList do begin
  85.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  86. { coercion does alias->fsspec }
  87.                 if ooe = noErr then
  88.                     ooe := DoDocs(myFSS, dodocp);
  89.             end;
  90.             ooe := AEDisposeDesc(docList);
  91.         end;
  92.         HandleDocs := oe;
  93.     end; { HandleDocs }
  94.  
  95.     function HandleQUIT (theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr;        { <aevt> }
  96.         var
  97.             oe: OSErr;
  98.             errStr: Str255;
  99.             willQuit: Boolean;                { did the user allow the quit or cancel }
  100.     begin
  101.     { We don't expect any params at all, but check in case the client requires any }
  102.         oe := GotRequiredParams(theAppleEvent);
  103.         oe := DoQuit(quitp);            { set global boolean: app will exit at end of event loop }
  104.         if reply.dataHandle <> nil then            { a reply is sought }
  105.             begin
  106.             if oe = noErr then
  107.                 errStr := 'OK'
  108.             else
  109.                 errStr := 'user cancelled quit';
  110.             oe := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  111.         end;
  112.         HandleQUIT := oe;
  113.     end;
  114.  
  115. {$S Init}
  116.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  117.         var
  118.             aevtErr: OSErr;
  119.     begin
  120.         aevtErr := noErr;
  121.         if (aevtErr = noErr) and (DoOApp <> nil) then
  122.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longInt(DoOApp), false);
  123.         if (aevtErr = noErr) and (DoODoc <> nil) then
  124.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longInt(DoODoc), false);
  125.         if (aevtErr = noErr) and (DoPrint <> nil) then
  126.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longInt(DoPrint), false);
  127.         if (aevtErr = noErr) and (DoQuit <> nil) then
  128.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longInt(DoQuit), false);
  129.         InitAppleEvents := aevtErr;
  130.     end;
  131.  
  132. end.